var g, _extends = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var i, a = arguments[e];
        for (i in a) Object.prototype.hasOwnProperty.call(a, i) && (t[i] = a[i]);
    }
    return t;
}, _tools = require("../../../utils/tools"), app = getApp(), page = app.page, request = app.util.request, route = app.route, toast = app.toast;

page({
    data: {
        src: app.srcRoot,
        formats: {},
        editorHeight: 300,
        keyboardHeight: 0,
        isIOS: !1,
        item: [ "redo", "undo", "indent", "outdent", "fontsize", "format-header-1", "format-header-2", "format-header-3", "format-header-4", "format-header-5", "format-header-6", "clearup", "preview", "date", "fontbgcolor", "clearedformat", "font", "723bianjiqi_duanhouju", "722bianjiqi_duanqianju", "text_color", "checklist", "baocun", "line-height", "quanping", "direction-rtl", "direction-ltr", "selectall", "fuzhi", "shanchu", "bianjisekuai", "fengexian", "dianzan", "charulianjie", "charutupian", "wuxupailie", "juzhongduiqi", "yinyong", "youxupailie", "youduiqi", "zitidaima", "xiaolian", "zitijiacu", "zitishanchuxian", "zitishangbiao", "zitibiaoti", "zitixiahuaxian", "zitixieti", "zitiyanse", "zuoduiqi", "zitiyulan", "zitixiabiao", "zuoyouduiqi", "duigoux", "guanbi", "shengyin_shiti", "Character-Spacing" ],
        classIndex: 0,
        cursor: 0,
        content: "",
        state: 1
    },
    onLoad: function(t) {
        (g = this).setData(_extends({}, app.item), app.item = "");
        var e = "ios" === wx.getSystemInfoSync().platform;
        g.setData({
            isIOS: e
        }), g.updatePosition(0);
        var i = 0;
        wx.onKeyboardHeightChange(function(t) {
            var e;
            t.height !== i && (e = 0 < t.height ? 1e3 * t.duration : 0, i = t.height, setTimeout(function() {
                wx.pageScrollTo({
                    scrollTop: 0,
                    success: function() {
                        g.updatePosition(i), g.editor.scrollIntoView();
                    }
                });
            }, e));
        }), request({
            url: "MLawArticleType",
            data: {
                user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
            },
            success: function(t) {
                if (g.data.type_id) for (var e = 0; e < t.length; e++) if (t[e].id == g.data.type_id) {
                    g.setData({
                        classIndex: e
                    });
                    break;
                }
                g.setData({
                    classList: t
                });
            }
        });
    },
    onPullDownRefresh: function() {},
    chooseClass: function() {
        g.setData({
            show: !0
        });
    },
    ChooseImage: function(t) {
        wx.chooseImage({
            count: 1,
            success: function(t) {
                g.setData({
                    s_img: t.tempFilePaths
                });
            }
        });
    },
    onEditorReady: function() {
        var e = this;
        wx.createSelectorQuery().select("#editor").context(function(t) {
            e.editor = t.context, e.editor.setContents({
                html: g.data.content || ""
            });
        }).exec();
    },
    readOnlyChange: function() {
        this.setData({
            readOnly: !this.data.readOnly
        });
    },
    updatePosition: function(t) {
        var e = wx.getSystemInfoSync(), i = e.windowHeight, i = (e.platform, 0 < t ? i - t - 50 : i);
        this.setData({
            editorHeight: i,
            keyboardHeight: t
        });
    },
    format: function(t) {
        var e = t.target.dataset, t = e.name, e = e.value;
        t && ("insertDivider" == t ? this.editor.insertDivider() : "undo" == t ? this.editor.undo() : this.editor.format(t, e));
    },
    onStatusChange: function(t) {
        t = t.detail;
        this.setData({
            formats: t
        });
    },
    insertImage: function() {
        wx.chooseImage({
            count: 9,
            success: function(t) {
                t = t.tempFilePaths;
                wx.showLoading({
                    title: "图片上传中..."
                }), (0, _tools.uploadImg)(t, function(t) {
                    wx.hideLoading();
                    for (var e = 0; e < t.length; e++) g.editor.insertImage({
                        src: g.data.system.attachurl + t[e],
                        width: "100%"
                    });
                }, !0);
            }
        });
    },
    getEditorContent: function(t) {
        g.setData({
            content: t.detail.html
        });
    },
    hideModal: function() {
        g.setData({
            show: !1
        });
    },
    chooseItem: function(t) {
        g.setData({
            classIndex: t.currentTarget.dataset.index
        });
    },
    titleInput: function(t) {
        g.setData({
            cursor: t.detail.cursor
        });
    },
    submitEdit: function(t) {
        var e = (t = t.detail.value).title, i = t.sort, a = t.state, o = g.data, n = o.content, s = o.s_img || "", r = o.classList[o.classIndex].id, u = o.id || "";
        return e ? s || o.img ? n ? (wx.showLoading({
            title: "图片上传中..."
        }), void (0, _tools.uploadImg)(s, function(t) {
            s = t ? g.data.system.attachurl + t : o.img, request({
                url: "MLawArticleAdd",
                data: {
                    user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                    type_id: r,
                    title: e,
                    img: s,
                    content: n,
                    sort: i || 0,
                    state: a ? 1 : 0,
                    id: u
                },
                success: function(t) {
                    1 == t ? (toast((g.data.id ? "修改" : "添加") + "成功"), setTimeout(function() {
                        route({
                            type: "navigateBack",
                            delta: 1
                        });
                    }, 1500)) : toast((g.data.id ? "修改" : "添加") + "失败:" + t);
                }
            });
        })) : toast("请输入文章内容") : toast("请选择文章封面") : toast("请输入文章标题");
    }
});